package com.baimawangzi.recovery.servlet.user;

import com.baimawangzi.recovery.bean.User;
import com.baimawangzi.recovery.service.impl.UserServiveImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/useradd.do")
public class UserAddServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserServiveImpl userServive = new UserServiveImpl();
        //解析url参数
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String nickname=req.getParameter("nickname");
        String gender=req.getParameter("gender");
        String sbirthday=req.getParameter("birthday");
        String phonenum=req.getParameter("phonenum");
        //时间转换
        Date birthday=null;

        try {
            birthday = new SimpleDateFormat("yyyy-MM-dd").parse(sbirthday);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //new java.sql.Date(birthday.getTime());//重点
        User user = new User(0, username, password, nickname, gender, new java.sql.Date(birthday.getTime()), Long.parseLong(phonenum), 0,new java.sql.Date(System.currentTimeMillis()));
        System.out.println("adduser"+user);
        int a = userServive.add(user);
        if(a>0){//返回的提示信息
            System.out.println("成功添加用户");
            req.setAttribute("msg","成功添加用户");
        }
        else {
            req.setAttribute("msg","失败添加用户请检查输入的数据");
        }

        req.getRequestDispatcher("userlist.do").forward(req,resp);
    }
}
